package com.example.android427;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.android427.Fragment.jiyiFragment1;
import com.example.android427.Fragment.jiyiFragment2;
import com.example.android427.Fragment.jiyiFragment3;

import java.util.ArrayList;
import java.util.List;

public class jiyiActivity extends AppCompatActivity implements View.OnClickListener{
    private Spinner spinner;
    private ViewPager mviewpager;
    private FragmentPagerAdapter mAdpter; //适配器
    private List<Fragment> mFragments; //数据源

    private LinearLayout mymemory;//书刊  我的
    private LinearLayout zuirememory;//最热
    private LinearLayout fenleimemory;//分类

    private TextView tmy;
    private TextView tzuire;
    private TextView tfenlei;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.jiyiactivity_main);
        spinner = findViewById(R.id.one);
        initView();//初始化控件
        initEvents();//为四个LinearLayout添加点击事件
        setSelect(0);  //让第一个fragment进行显示
    }

    private void initEvents() {  //为三个按钮设置点击事件
        mymemory.setOnClickListener(this);
        zuirememory.setOnClickListener(this);
        fenleimemory.setOnClickListener(this);
    }

    private void initView() {  //初始化
        mviewpager = (ViewPager) findViewById(R.id.id_content);//关联viewpager

        mymemory = (LinearLayout) findViewById(R.id.mymemory);//初始化LinearLayout
        zuirememory = (LinearLayout) findViewById(R.id.zuirememory);
        fenleimemory = (LinearLayout) findViewById(R.id.fenleimemory);

        tmy =findViewById(R.id.t1);//初始化图标按钮
        tzuire = findViewById(R.id.t2);
        tfenlei = findViewById(R.id.t3);


        mFragments = new ArrayList<Fragment>();  //初始化数据源
        Fragment myjiyi = new jiyiFragment1();
        Fragment zuirejiyi = new jiyiFragment2();
        Fragment fenleijiyi = new jiyiFragment3();

        mFragments.add(myjiyi);
        mFragments.add(zuirejiyi);
        mFragments.add(fenleijiyi);


        mAdpter = new FragmentPagerAdapter(getSupportFragmentManager()) { //设置适配器
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return mFragments.get(position);
            }//返回不同的fragment
            @Override
            public int getCount() {
                return mFragments.size();
            }//返回数据源的数量
        };
        mviewpager.setAdapter(mAdpter);
        mviewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }
            @Override
            public void onPageSelected(int position) {
                int currentItem = mviewpager.getCurrentItem();
                setTab(currentItem);
            }
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    private void setSelect(int i) {
        setTab(i);
        mviewpager.setCurrentItem(i);
    }

    private void setTab(int i) {  //切换图片
        resetImgs();
        switch (i) {
            case 0:
                tmy.setTextColor(getResources().getColor(R.color.colorTextViewPress));
                break;
            case 1:
                tzuire.setTextColor(getResources().getColor(R.color.colorTextViewPress));
                break;
            case 2:
                tfenlei.setTextColor(getResources().getColor(R.color.colorTextViewPress));
                break;
            default:
                break;
        }
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.mymemory:
                setSelect(0);
                break;
            case R.id.zuirememory:
                setSelect(1);
                break;
            case R.id.fenleimemory:
                setSelect(2);
                break;
        }
    }

    //切换图片至暗色
    private void resetImgs() {
        tmy.setTextColor(getResources().getColor(R.color.colorTextViewNormal));
        tzuire.setTextColor(getResources().getColor(R.color.colorTextViewNormal));
        tfenlei.setTextColor(getResources().getColor(R.color.colorTextViewNormal));
    }


}


